 //控制层 
app.controller('typeTemplateController' ,function($scope,$controller,typeTemplateService,brandServcie,specificationService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		typeTemplateService.findAll().success(
			function(response){
				$scope.list=response;
			}
		);
	}

	//查询品牌列表
	$scope.brandList = {
		data: []
	};

	//查询规格列表
	$scope.specList = {
		data: []
	};

	//初始化时查询列表
	$scope.getBrandList = function() {

		brandServcie.findList().success(
			function (res) {
				$scope.brandList.data=res;
			}
		);

	}

	//初始化时查询规格列表
	$scope.findOptions = function() {

		specificationService.findOptions().success(
			function (res) {
				$scope.specList.data=res;
			}
		);

	}

	
	//分页
	$scope.findPage=function(page,rows){			
		typeTemplateService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}


	//新增扩展属性
	$scope.addAttr = function() {
		$scope.entity.customAttributeItems.push({});
	}

	//移除扩展属性
	$scope.removeOption = function (index) {
		$scope.entity.customAttributeItems.splice(index,1);
	}

	/*定义实体，用于保存*/
	$scope.entity = {

		name: '',
		brandIds: null,
		specIds: null,
		customAttributeItems: []

	};

	//查询实体 
	$scope.findOne=function(id){				
		typeTemplateService.findOne(id).success(
			function(response){
				//转换数据格式
				$scope.entity.brandIds=JSON.parse(response.brandIds);
				$scope.entity.specIds=JSON.parse(response.specIds);
				$scope.entity.customAttributeItems=JSON.parse(response.customAttributeItems);
				$scope.entity.name=response.name;
				$scope.entity.id=response.id;
			}
		);				
	}

	$scope.brandIds;
	$scope.specIds;

	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=typeTemplateService.update( $scope.entity ); //修改  
		}else{
			serviceObject=typeTemplateService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){
		//获取选中的复选框
		typeTemplateService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.reloadList();//刷新列表
					$scope.selectIds=[];
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){
		typeTemplateService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}
		);
	}
    
});	
